package com.winway.sixbook;


import java.text.SimpleDateFormat;
import java.util.Date;

import com.winway.sixbook.custom.CloseActivityClass;
import com.winway.sixbook.service.RecordDataManager;
import com.winway.sixbook.ui.CalendarView;
import com.winway.sixbook.ui.CounterListActivity;
import com.winway.sixbook.ui.GridActivity;
import com.winway.sixbook.ui.GridViewChooose;
import com.winway.sixbook.ui.SetActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class HomeActivity extends Activity {
	
	private static final String TAG = "HomeActivity";
	private ImageButton doctor_top;
	private ImageButton zuoye_top;
	private ImageButton secret_top;
	private ImageButton more_top;
	private ImageButton imgButton_ask;
	AlertDialog builder = null;
	AppContext appState ;
	RecordDataManager recordDataManager ;
	private long firstTime=0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		CloseActivityClass.activityList.add(this);
		
		appState = ((AppContext)getApplicationContext());
		appState.setSysstate("OK");
		Log.v(TAG, appState.getSysstate());
		if (recordDataManager == null) {
        	recordDataManager = new RecordDataManager(this);
        	recordDataManager.openDataBase();
        }
		//初始化
		mainInit();
	}
	
	private void mainInit(){
		doctor_top = (ImageButton)findViewById(R.id.doctor_top);
		zuoye_top = (ImageButton)findViewById(R.id.zuoye_top);
		secret_top = (ImageButton)findViewById(R.id.secret_top);
		more_top = (ImageButton)findViewById(R.id.more_top);
		imgButton_ask = (ImageButton)findViewById(R.id.imgButton_ask);
		
		doctor_top.setOnClickListener(mListener);
		zuoye_top.setOnClickListener(mListener);
		secret_top.setOnClickListener(mListener);
		more_top.setOnClickListener(mListener);
		imgButton_ask.setOnClickListener(mListener);
	}
	OnClickListener mListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.doctor_top:
					//计数
					int count=recordDataManager.findCountTableCount();
					if(count>0){
						Intent intentset = new Intent(HomeActivity.this, CounterListActivity.class);
						startActivity(intentset);
					}else{
						Toast.makeText(HomeActivity.this, R.string.counttype_empty,Toast.LENGTH_SHORT).show();
						return ;
					}
					break;
				case R.id.zuoye_top:
					//历史
//					Intent intent2 = new Intent(HomeActivity.this, CalendarActivity.class);
					Intent intent2 = new Intent(HomeActivity.this, CalendarView.class);
					startActivity(intent2);
					break;
				case R.id.secret_top:
					//今天
//					Intent intent3 = new Intent(HomeActivity.this, TodayListNewActivity.class);
					Intent intent3 = new Intent(HomeActivity.this, GridActivity.class);
					Bundle bundle = new Bundle(); 
					SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd");
					bundle.putString("Addtime", dateformat1.format(new Date()));
					intent3.putExtras(bundle); 
					startActivity(intent3);
					break;
				case R.id.more_top:
					//设置
					Intent counter = new Intent(HomeActivity.this, SetActivity.class);
					startActivity(counter);
					break;
				case R.id.imgButton_ask:
					//我要添加按钮
					int count1=recordDataManager.findSeedsCount();
					if(count1>0){
						Intent counter4 = new Intent(HomeActivity.this, GridViewChooose.class);
						startActivity(counter4);
						finish();
					}else{
						Toast.makeText(HomeActivity.this, R.string.type_empty,Toast.LENGTH_SHORT).show();
						return ;
					}
					break;
			}
		}
	};
	
	
//	@Override
//	public boolean onKeyUp(int keyCode, KeyEvent event) {
//		
//      if(keyCode == KeyEvent.KEYCODE_BACK){
//    	  long secondTime = System.currentTimeMillis();
//    	  
//          if (secondTime - firstTime > 2000) {
//        	  //如果两次按键时间间隔大于2秒，则不退出  
//          }else{
//        	  System.out.println("tuichu");
//        	  builder = new AlertDialog.Builder(HomeActivity.this)
//				.setIcon(R.drawable.ic_launcher)
//				.setTitle("温馨提示：")
//				.setMessage("您是否要退出该程序！")
//				.setPositiveButton("确定",
//						new DialogInterface.OnClickListener() {
//							@Override
//							public void onClick(DialogInterface dialog, int whichButton) {
//								HomeActivity.this.finish();
//								appState.setSysstate("nook");
//								CloseActivityClass.exitClient(HomeActivity.this); 
//								Log.v(TAG, appState.getSysstate());
//							}
//						}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
//							@Override
//							public void onClick(DialogInterface dialog,
//									int whichButton) {
//								builder.dismiss();
//							}
//						}).show();
//          }
//		
//      }
//      return true;
//	}
	
	private int mBackKeyPressedTimes = 0;

    @Override
    public void onBackPressed() {
        if (mBackKeyPressedTimes == 0) {
                Toast.makeText(this, "再按一次退出程序 ", Toast.LENGTH_SHORT).show();
                mBackKeyPressedTimes = 1;
                new Thread() {
                        @Override
                        public void run() {
                                try {
                                        Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                        e.printStackTrace();
                                } finally {
                                        mBackKeyPressedTimes = 0;
                                }
                        }
                }.start();
                return;
        }else{
        	CloseActivityClass.exitClient(HomeActivity.this);
        	HomeActivity.this.finish();
        	appState.setSysstate("nook");
        }
        super.onBackPressed();
    }


	@Override
	protected void onResume() {
		if (recordDataManager == null) {
			recordDataManager = new RecordDataManager(this);
			recordDataManager.openDataBase();
        }
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (recordDataManager != null) {
			recordDataManager.closeDataBase();
			recordDataManager = null;
        }
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (recordDataManager != null) {
			recordDataManager.closeDataBase();
			recordDataManager = null;
        }
		super.onPause();
	}
}
